Release 10.1A: OpenEdge Development:
Programming Interfaces


Reading XML into a ProDataSet

XML information read into a ProDataSet includes:

In this first code example, two static temp-tables are defined in an include file:

/* pi-tfx-ttSetup-7.i */ 
/* Creates two static temp-tables for use in a ProDataSet. */ 
/* Definition for temp-table ttCust */ 
DEFINE TEMP-TABLE ttCust  
    BEFORE-TABLE ttCustBefore 
	        FIELD CustNum LIKE Customer.CustNum 
        FIELD Name LIKE Customer.Name  
        FIELD Country LIKE Customer.Country 
        FIELD Comments LIKE Customer.Comments FORMAT "x(40)" 
        INDEX CustNum IS PRIMARY UNIQUE CustNum 
        INDEX Name Name 
        INDEX Comments IS WORD-INDEX Comments. 
/* Definition for temp-table ttOrder */ 
DEFINE TEMP-TABLE ttOrd 
    FIELD OrderNum LIKE Order.OrderNum 
    FIELD CustNum LIKE Order.CustNum 
    FIELD OrderDate LIKE Order.OrderDate 
    INDEX OrderNum IS PRIMARY UNIQUE OrderNum 
    INDEX CustOrder IS UNIQUE CustNum OrderNum 
    INDEX OrderDate OrderDate. 

Here is a snippet of the XML file:

<?xml version="1.0"?> 
<dsCustomerOrders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
  <ttCust> 
    <CustNum>1</CustNum> 
    <Country>USA</Country> 
    <Name>Lift Tours</Name> 
    <Address>276 North Drive</Address> 
    <Address2/> 
    <City>Burlington</City> 
    <State>MA</State> 
    <PostalCode>01730</PostalCode> 
    <Contact>Gloria Shepley</Contact> 
    <Phone>(617) 450-0086</Phone> 
    <SalesRep>HXM</SalesRep> 
    <CreditLimit>66700.0</CreditLimit> 
    <Balance>903.64</Balance> 
    <Terms>Net30</Terms> 
    <Discount>35</Discount> 
    <Comments>This customer is on credit hold.</Comments> 
    <Fax/> 
    <EmailAddress/> 
    <ttOrd> 
      <Ordernum>6</Ordernum> 
      <CustNum>1</CustNum> 
      <OrderDate>1998-02-11</OrderDate> 
      <ShipDate>1998-02-16</ShipDate> 
      <PromiseDate>1998-02-16</PromiseDate> 
      <Carrier>Standard Mail</Carrier> 
      <Instructions/> 
      <PO/> 
      <Terms>Net30</Terms> 
      <SalesRep>HXM</SalesRep> 
      <BillToID>0</BillToID> 
      <ShipToID>0</ShipToID> 
      <OrderStatus>Shipped</OrderStatus> 
      <WarehouseNum>0</WarehouseNum> 
      <Creditcard>American Express</Creditcard> 
    </ttOrd> 
. 
. 
. 

Note: The child records are nested within the parent records.

The code sample defines the static ProDataSet, reads the previous XML file, and reports all the orders by customer number:

/* pi-tfx-read-7.p */ 
/* Populates a static ProDataSet with records from an XML file. */ 
{pi-tfx-parameterVarDefs.i} 
{pi-tfx-ttSetup-7.i} 
DEFINE VARIABLE returnValue AS LOGICAL NO-UNDO. 
DEFINE VARIABLE hPDS AS HANDLE. 
DEFINE DATASET dsCustomerOrders  
    FOR ttCust, ttOrd 
    DATA-RELATION custOrd FOR ttCust, ttOrd  
    RELATION-FIELDS(custNum, CustNum) NESTED. 
hPDS = DATASET dsCustomerOrders:HANDLE. 
ASSIGN 
    cSourceType = "FILE" 
    cFile = "dsCustomerOrders.xml" 
    cReadMode = "EMPTY" 
    cSchemaLocation = ? 
    lOverrideDefaultMapping = NO. 
returnValue = hPDS:READ-XML (cSourceType, cFile, cReadMode, 
                             cSchemaLocation, lOverrideDefaultMapping). 
IF returnValue THEN DO: 
    FOR EACH ttCust BY CustNum:  
        FOR EACH ttOrd WHERE ttOrd.CustNum = ttCust.CustNum: 
            DISPLAY ttCust.CustNum ttCust.NAME FORMAT "x(30)" ttOrd.OrderNum. 
        END. 
    END. 
END. 

This code displays a report of all the orders for each customer, like this one:

The next code sample provides a dynamic ProDataSet handle with XML Schema and data.

/* pi-tfx-read-8.p */ 
/* Populates an empty dynamic ProDataSet with XML Schema and records  
   from an XML document. */ 
   
{pi-tfx-parameterVarDefs.i} 
DEFINE VARIABLE returnValue AS LOGICAL NO-UNDO. 
DEFINE VARIABLE hPDS AS HANDLE NO-UNDO. 
CREATE DATASET hPDS. 
ASSIGN 
     cSourceType = "FILE" 
     cFile = "Dept400-2.xml"  
     cReadMode = "EMPTY" 
     cSchemaLocation = "Dept400-2.xsd" 
     lOverrideDefaultMapping = ? 
     cFieldTypeMapping = ? 
     cVerifySchemaMode = ?. 
  
returnValue = hPDS:READ-XML(cSourceType, cFile, cReadMode, cSchemaLocation, 
                            lOverrideDefaultMapping, cFieldTypeMapping, 
                            cVerifySchemaMode). 
IF returnValue THEN 
    DISPLAY SKIP "How many temp-table buffers in the ProDataSet?"  
                 hPDS:NUM-BUFFERS SKIP 
                 "How many data-relations in the ProDataSet?"  
                 hPDS:NUM-RELATIONS SKIP(2). 
    DEFINE VARIABLE hQuery AS HANDLE NO-UNDO. 
    CREATE QUERY hQuery. 
    hQuery:SET-BUFFERS(hPDS:GET-BUFFER-HANDLE("ttEmp")). 
    hQuery:QUERY-PREPARE("FOR EACH ttEmp"). 
    hQuery:QUERY-OPEN(). 
    hQuery:GET-FIRST(). 
    DISPLAY "Displaying Employee Roster..." SKIP(2) 
            "Employee No.  Last Name        First Name". 
    REPEAT WHILE NOT hQuery:QUERY-OFF-END: 
        DISPLAY hPDS:GET-BUFFER-HANDLE("ttEmp"):BUFFER-FIELD("EmpNum") 
                                                         :BUFFER-VALUE 
                hPDS:GET-BUFFER-HANDLE("ttEmp"):BUFFER-FIELD("LastName") 
                                                         :BUFFER-VALUE  
                hPDS:GET-BUFFER-HANDLE("ttEmp"):BUFFER-FIELD("FirstName") 
                                                         :BUFFER-VALUE. 
        hQuery:GET-NEXT(). 
END. 

The output for this code is shown below:

In this sample, a handle was defined and used to create a new dynamic ProDataSet. The ProDataSet is in the CLEAR state at this point, meaning it has no 4GL definition. When the code calls the READ-XML() method, the method sees that XML Schema has been provided. Since the dynamic ProDataSet is in the clear state, it knows the schema is being provided to create 4GL definitions for the object from the XML Schema. If the ProDataSet had not been in the CLEAR state, the method would have know to verify the XML Schema against whatever 4GL definition existed for the ProDataSet.

The method can proceed to read in the data from the XML document provided in the method call. The code displays the number of temp-tables and data-relations in the dynamic object as well as a summary of the employee records now in the ttEmp temp-table.


Copyright © 2005 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095